package cn.jiedh.app.meiweidk.strategy;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.jiedh.app.meiweidk.MyWebActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.UI.EmptyRecyclerView;
import cn.jiedh.app.meiweidk.Utils.UI.StateLayout;
import cn.jiedh.app.meiweidk.Utils.UI.VerticalSpaceItemDecoration;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseFragment;
import cn.jiedh.app.meiweidk.baseadapter.BaseAdapter;
import cn.jiedh.app.meiweidk.baseadapter.BaseViewHolder;
import cn.jiedh.app.meiweidk.buried.BuriedBean;
import cn.jiedh.app.meiweidk.entity.StrategyListBean;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;

/**
 * @name：攻略列表
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/21
 */
public class FragmentStrategy extends BaseFragment {
    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.bar_img_back)
    ImageView back;
    @BindView(R.id.strategy_rv)
    EmptyRecyclerView rv;
    @BindView(R.id.state_layout)
    StateLayout stateLayout;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.nn_btn)
    TextView emptyTv;

    private List<StrategyListBean.DataList> list = new ArrayList<>();
    private BaseAdapter<StrategyListBean.DataList> adapter;
    private TextView strategyHeaderTitleTv;
    private TextView strategyHeaderTimeTv;

    private int pageSize = 10;
    private int pageNo = 1;

    private int headerTypeId;
    private int headerRaidersId;
    private String headerTitle;
    private String headerUrl;
    private View view;
    private View headerView;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_strategy;
    }

    @Override
    protected void initView(View view) {
        this.view = view;
        title.setText("攻略");
        emptyTv.setText("敬请期待");
        back.setVisibility(View.INVISIBLE);

        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new BaseAdapter<StrategyListBean.DataList>(getActivity(), R.layout.strategy_item, list) {
            @Override
            public void convert(BaseViewHolder holder, StrategyListBean.DataList strategyBean, int position) {
                holder.setImageUrl(R.id.strategy_icon, strategyBean.getImgUrl());
                holder.setText(R.id.strategy_title, strategyBean.getTitle());
                holder.setText(R.id.strategy_time, strategyBean.getStartTime());
                holder.setOnclickListener(R.id.strategy_item, v1 -> {
                    MobclickAgent.onEvent(getActivity(), "Raiders" + (position + 1)); //umeng
                    List<BuriedBean> buriedList = new ArrayList<>();
                    buriedList.add(new BuriedBean("click", "攻略列表" + strategyBean.getTitle(), "3", strategyBean.getRaidersId() + "", strategyBean.getTitle(), "攻略列表", "false", "", "", "", "", "", "", "", "", "", ""));
                    SPUtils.saveBuriedList(buriedList);
                    if (0 == strategyBean.getType()) {
                        Bundle webBundle = new Bundle();
                        webBundle.putString("url", strategyBean.getLink());
                        webBundle.putString("title", strategyBean.getTitle());
                        ActivityUtil.next(getActivity(), MyWebActivity.class, webBundle);
                    } else if (1 == strategyBean.getType()) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", strategyBean.getTitle());
                        bundle.putInt("raidersId", strategyBean.getRaidersId());
                        ActivityUtil.next(getActivity(), StrategyActivity.class, bundle);
                    }
                });
            }
        };
        headerView = LayoutInflater.from(getContext()).inflate(R.layout.strategy_header, rv, false);
        adapter.setHeaderView(headerView);
        initHeader(headerView);
        rv.addItemDecoration(new VerticalSpaceItemDecoration(2, false));
        rv.setAdapter(adapter);
        refreshAndLoadMore();

        stateLayout.setOnNoClick(view1 -> refreshLayout.autoRefresh());
    }

    private void refreshAndLoadMore() {
        refreshLayout.setOnRefreshListener(refreshLayout -> refreshLayout.getLayout().postDelayed(() -> {
            pageNo = 1;
            list.clear();
            stateLayout.netviewGone();
            getData(pageNo);
            refreshLayout.finishRefresh();
            refreshLayout.setLoadmoreFinished(false);
        }, 2000));
        refreshLayout.setOnLoadmoreListener(refreshLayout -> {
            refreshLayout.getLayout().postDelayed(() -> {
                pageNo++;
                getData(pageNo);
                refreshLayout.finishLoadmore();
            }, 2000);

        });
    }

    private void initHeader(View headerView) {

        strategyHeaderTitleTv = headerView.findViewById(R.id.strategy_header_title);
        strategyHeaderTimeTv = headerView.findViewById(R.id.strategy_header_time);

        headerView.setOnClickListener(v -> {
            if (1 == headerTypeId) {
                Bundle bundle = new Bundle();
                bundle.putInt("raidersId", headerRaidersId);
                bundle.putString("title", headerTitle);
                ActivityUtil.next(getActivity(), StrategyActivity.class, bundle);
            } else if (0 == headerTypeId) {
                Bundle webBundle = new Bundle();
                webBundle.putString("url", headerUrl);
                webBundle.putString("title", headerTitle);
                ActivityUtil.next(getActivity(), MyWebActivity.class, webBundle);
            }
        });
    }


    @Override
    protected void initData() {
        getData(1);
    }

    private void getData(int mPageNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNo", mPageNo);
        request(UrlUtils.RAIDERSLIST, map, false, new ResponseSuccess<StrategyListBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<StrategyListBean> response) {
                if (0 == response.getCode()) {
                    if (mPageNo == 1) {
                        List<StrategyListBean.DataList> mList = response.getData().getDataList();
                        if (mList.size() > 0) {
                            strategyHeaderTitleTv.setText(mList.get(0).getTitle());
                            strategyHeaderTimeTv.setText(mList.get(0).getStartTime());
                            headerTypeId = mList.get(0).getType();
                            headerRaidersId = mList.get(0).getRaidersId();
                            headerTitle = mList.get(0).getTitle();
                            if (headerTypeId == 0) {
                                headerUrl = mList.get(0).getLink();
                            }
                            mList.remove(0);
                            list.addAll(mList);
                            adapter.setHeaderView(headerView);
                        } else {
                            if (list.size() == 0) {
                                adapter.setHeaderView(null);
                                rv.setEmptyView(view.findViewById(R.id.empty_view));
                            }
                        }
                    } else {
                        if (response.getData().getDataList().size() <= 0) {
                            pageNo = mPageNo - 1;
                        }
                        list.addAll(response.getData().getDataList());
                    }
                    adapter.notifyDataSetChanged();
                }

            }
        }, (error, msg) -> {
            list.clear();
            adapter.setHeaderView(null);
            rv.setEmptyView(view.findViewById(R.id.empty_view));
            adapter.notifyDataSetChanged();
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
    }
}
